qx.Class.define("caraffa.forms.StockMensual",
{
extend : qx.ui.form.ow.FormTab,
construct : function (mesMostrar)
{
	//RPC
	this._rpcSMes = new qx.io.remote.Rpc();
	this._rpcSMes.setTimeout(10000);
	this._rpcSMes.setUrl("services/");
	this._rpcSMes.setServiceName("stockMensual");
	
	this._grpStockMensual = new qx.ui.groupbox.GroupBox();
		this._grpStockMensual.setLayout(new qx.ui.layout.Basic());		
	
		this._txtMes = new qx.ui.form.ow.TextField("Mes:<br><small>(mm/aa)</small>");
			this._txtMes.setValue(mesMostrar);
			this._txtMes.getLabel().setWidth(50);
			this._txtMes.getText().setWidth(50);

		this._txtFiltro = new qx.ui.form.ow.TextField("Filtro:");
			this._txtFiltro.getLabel().setWidth(50);
		this._cmbRubro = new qx.ui.form.ow.ComboBox("","",true);
			this._cmbRubro.getLabel().setWidth(0);
		this._btnRefrescar = new qx.ui.form.Button("Refrescar", "caraffa/16/actualizar.png");
		
		this._mdlStock = new qx.ui.table.model.Simple();
			this._mdlStock.setColumns(["Código", "Fec.Egreso","Fec.Reg.","Rubro", "Artículo", "Marca", "Cantidad", "Motivo", "Usuario" ]);
		this._tblStock = new qx.ui.table.Table(this._mdlStock);
			this._tblStock.setStatusBarVisible(false);
			this._tblStock.setColumnWidth(0, 80);
			this._tblStock.setColumnWidth(1, 80);
			this._tblStock.setColumnWidth(2, 80);
				this._tblStock.getTableColumnModel().setColumnVisible(2,false);
			this._tblStock.setColumnWidth(3, 120);
			this._tblStock.setColumnWidth(4, 270);
			this._tblStock.setColumnWidth(5, 120);
			this._tblStock.setColumnWidth(6, 80);
			this._tblStock.setColumnWidth(7, 200);
			this._tblStock.setColumnWidth(8, 80);
				this._tblStock.getTableColumnModel().setColumnVisible(8,false);
			this._tblStock.setShowCellFocusIndicator(false);
			this._tblStock.setWidth(960);
			this._tblStock.setHeight(250);


	this._grpStockMensual.add(this._txtMes,{top:0, left:0});			
	this._grpStockMensual.add(this._tblStock,{top:50, left:0});
	this._grpStockMensual.add(this._txtFiltro,{top:0, left:110});			
	this._grpStockMensual.add(this._cmbRubro,{top:0, left:400});
	this._grpStockMensual.add(this._btnRefrescar,{top:0, left:650});	

	this._grpStockMensual1 = new qx.ui.groupbox.GroupBox();
		this._grpStockMensual1.setLayout(new qx.ui.layout.Basic());
		this._grpStockMensual1.setWidth(700);
		this._txaDetalle = new qx.ui.form.ow.TextArea("Motivo:","","",false);
			this._txaDetalle.getText().setWidth(670);
			this._txaDetalle.getText().setHeight(120);
			this._txaDetalle.getText().setReadOnly(true);
			
	this._grpStockMensual1.add(this._txaDetalle,{top:0,left:0});

	this.base(arguments, "StockMes", "Stock: Mov.Mensual");
	
	this.add(this._grpStockMensual,{top:0,left:0});
	this.add(this._grpStockMensual1,{top:330,left:140});
	this._attachEvents();	
},

members: {
	_rpcSMes : null,
	
	_attachEvents : function (){
		this.addListener('appear', this._initForm, this);
		this._btnRefrescar.addListener('execute', this._tblStockRefresh, this);
		this._tblStock.addListener('cellClick', this._tblStockCellClick, this);
	},
	_initForm : function(){
		try{
			var resultRubros = this._rpcSMes.callSync("getRubros");
		}
		catch(ex){
			alert("Sync exception: " + ex);
		}
		this._cmbRubro.setNewValues(resultRubros, true);
		this._tblStockRefresh();
	},
	_tblStockRefresh : function(){
		try{
			var params = new Object();
				params.rubro = this._cmbRubro.getValue();
				params.filtro = this._txtFiltro.getValue();
				params.mes = this._txtMes.getValue();
			var result = this._rpcSMes.callSync("getStock", params);
			this._mdlStock.setData(result.values);
		}
		catch(ex){
			alert("Sync exception: " + ex);
		}
	},
	_tblStockCellClick : function(){
		var motivo = this._mdlStock.getValue(7, this._tblStock.getFocusedRow());
		var cod_operacion = this._mdlStock.getValue(0, this._tblStock.getFocusedRow());
		var fecha = this._mdlStock.getValue(1, this._tblStock.getFocusedRow());
		var usuario = this._mdlStock.getValue(8, this._tblStock.getFocusedRow())
		var mostrar = "Operación N°"+cod_operacion+ "Fecha:"+fecha+"      Usuario:"+usuario;
			mostrar+="\n-----------------------------------------------------------------------------------------------------------------------------------------\n";
			mostrar+=motivo;

		this._txaDetalle.setValue(mostrar);
	}
}
});
